#include "addfriend.h"
#include "ui_addfriend.h"

AddFriend::AddFriend(QString uName,QWidget *parent) :
    QWidget(parent),uName(uName),
    ui(new Ui::AddFriend)
{
    ui->setupUi(this);
    //和服务器创建新的链接
    //绑定接收的函数
    tcpClient=new QTcpSocket(this);
    QString LocalIP=getLocalIP();
    //连接服务器
    quint16 targetport=8888;
    tcpClient->connectToHost(LocalIP,targetport);
    connect(tcpClient, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
}

AddFriend::~AddFriend()
{
    delete ui;
}

QString AddFriend::getLocalIP()
{
    QString hostName=QHostInfo::localHostName();
    QHostInfo hostInfo=QHostInfo::fromName(hostName);
    QString localIP="";
    QList<QHostAddress> addList=hostInfo.addresses();
    if(!addList.isEmpty())
    {
        for(int i=0;i<addList.count();i++)
        {
            QHostAddress aHost=addList.at(i);
            if(QAbstractSocket::IPv4Protocol==aHost.protocol())
            {
                localIP=aHost.toString();
                break;
            }
        }
    }
    return localIP;
}

void AddFriend::on_commit_pushButton_clicked()//提交好友申请
{
    //获取输入的信息
    QString beizhu=ui->name_lineEdit->text();
    int friendtype=ui->friendtype_comboBox->currentIndex();
    QString friendgroup=ui->friendgroup_lineEdit->text();
    QString selected_user=ui->selectuser_label->text().split(":").at(1);//：后面的东西就是选中的用户名
    if(selected_user=="NULL")
    {
         QMessageBox::warning(0,tr("警告"),"请先选择需要添加为好友的用户",QMessageBox::Ok);
         return;
    }
    QByteArray data;
    QDataStream out(&data, QIODevice::WriteOnly);
    out<< quint8('A')<<quint8('1')<<selected_user<<uName<<beizhu<<friendtype<<friendgroup;//A1表示进行添加好友操作
    tcpClient->write(data);//发送数据
}

void AddFriend::processPendingDatagrams()//处理从服务器端发送的消息
{
    QByteArray datagram;
    datagram=tcpClient->readAll();
    QDataStream in(&datagram,QIODevice::ReadOnly);
    QString avail_username;
    quint8 type;
    QString info;
    in>>type;
    //
    if(type=='D')//数据库出错
    {
        in>>info;
        QMessageBox::warning(0,tr("警告"),"数据库出错"+info,QMessageBox::Ok);
    }
    else if(type=='A')//接收到可添加为好友的用户名
    {
        //先清空原来的内容：
        ui->avail_user_listWidget->clear();
        while(avail_username!="over")
        {
            in>>avail_username;
            //将接收到的数据添加到列表中
            if(avail_username!="over")
            ui->avail_user_listWidget->addItem(avail_username);
        }
    }
    else if(type=='S')//添加好友成功
    {
         QMessageBox::information(0,tr("提示"),"添加好友成功",QMessageBox::Ok);
//         this->close();
    }
    else
    {
        QMessageBox::warning(0,tr("警告"),"未知错误",QMessageBox::Ok);
    }

}


void AddFriend::on_refresh_pushButton_clicked()//刷新可申请的好友列表
{
    QByteArray data;
    QDataStream out(&data, QIODevice::WriteOnly);
    out<< quint8('S')<<quint8('1')<<uName;//S1表示向服务器获取所有未加好友
    tcpClient->write(data);//发送数据

}


void AddFriend::on_avail_user_listWidget_itemClicked(QListWidgetItem *item)
{
    auto pre=ui->selectuser_label->text().split(":").at(0);
    ui->selectuser_label->setText(pre+":"+item->text());

}

